﻿import com.dmotiko.general.*
import com.dmotiko.mirador.*
import com.dmotiko.util.*
import mx.transitions.*;
import mx.transitions.easing.*;
import mx.utils.Delegate;

class MiradorPhotoGalleryThumbs
extends BaseClip {
	private var mcBack:MovieClip;
	private var mcNext:BaseClip;
	private var mcPrev:BaseClip;
	private var mcMask:BaseClip;
	private var mcContainer:MovieClip;
	private var nThumbs:Number;
	private var aThumbs:Array;
	private var activeThumb:MiradorPhotoGalleryThumb;
	private var nPos:Number;
	private var aVisibles:Array;
		
	private function initClip():Void {
		super.initClip();
		//mcBack._alpha = 50;
		mcNext._visible = mcPrev._visible = false;
		nPos = 0;
	}
	
	private function refreshData():Void {
		
		//trace("MiradorPhotoGalleryThumbs | refreshData= " + data);
		
		if(!mcContainer) mcContainer = this.createEmptyMovieClip("mcContainer", this.getNextHighestDepth());
		else mcContainer = this.createEmptyMovieClip("mcContainer", mcContainer.getDepth() );
		mcNext._visible = mcPrev._visible = false;
		nPos = 0;
		
		mcContainer.setMask(mcMask);
		mcContainer._x = mcMask._x;
							
		aThumbs = new Array();
		nThumbs = 0;
		for (var i:Number = 0; i <  data.length; i++) {
			var thumb:PhotoClip = PhotoClip( mcContainer.attachMovie("MiradorPhotoGalleryThumb", "thumb" + i, i) );
			//trace("thumb= "+thumb);
			aThumbs.push(thumb);
			thumb.addEventListener( BaseClipEvent.READY, Delegate.create(this, thumbReady) );
			thumb.addEventListener( BaseClipEvent.CHANGED, Delegate.create(this, thumbChanged) );
			thumb.setData(data[i]);
			thumb._visible = false;
		}
		
	}
	
	private function thumbChanged(e):Void {
		if ( activeThumb ) activeThumb.setActive(false);
		activeThumb = e.target;
		activeThumb.setActive(true);
		dispatchEvent( { type: BaseClipEvent.CHANGED, target: this, data: e.data } );
	}
	
	private function thumbReady(e):Void {
		nThumbs++;
		if (nThumbs == data.length) showThumbs();
	}
	
	private function showThumbs():Void {
		var nX:Number = 0;
		mcContainer._x = mcMask._x + 10;
		mcContainer._y = mcMask._y + 3; // TOFIX= negrada total
		aVisibles = new Array();
		var lastX:Number;
		for (var i:Number = 0; i < aThumbs.length; i++) {
			var thumb:PhotoClip = aThumbs[i];
			thumb._visible = true;
			if ( mcContainer._x + nX + thumb._width > mcMask._x + mcMask._width ) {
				nX = mcMask._x + mcMask._width + thumb._width;
				//la ultima vez centro al contenedor
				if (!mcContainer.positioned) {
					mcContainer.positioned = true;
					mcContainer._x = mcMask._x + ( mcMask._width - lastX ) /2;
				}
			} else {
				aVisibles.push( { thumbIndex: i, thumb: thumb } );
				lastX = nX + thumb._width;
			}
			registerTween( "thumbMove" + i, new Tween( thumb, "_x", Elastic.easeOut, thumb._x, nX, 1, true), false, true );
			nX += thumb["mcMini"]._width + 5; // TOFIX= negrada total
		}
		aThumbs[0].onRelease();
		
	}
	
	private function checkScroll():Void	{
		if ( mcContainer._width > mcMask._width ) {
			var root = this;
			mcNext._visible = true;
			mcNext.onRelease = function() {
				root.nextPosition();
			}
			mcPrev.onRelease = function() {
				root.prevPosition();
			}
		}
	}
	
	private function nextPosition():Void {
		var thumb = aVisibles[0].thumb;
		thumb.stopMotion();
		registerTween( "thumbMove" + aVisibles[0].thumbIndex, new Tween( thumb, "_x", Strong.easeOut, thumb._x, - thumb._width*2, 0.5, true));
		aVisibles.shift();
		var nextThumb:Number = aVisibles[ aVisibles.length - 1 ].thumbIndex + 1;
		if ( aThumbs[nextThumb] ) aVisibles.push( { thumbIndex: nextThumb, thumb: aThumbs[ nextThumb ] } );
		if ( !aThumbs[nextThumb+1] ) mcNext._visible = false;
		var nX:Number = 0;
		for (var i:Number = 0; i < aVisibles.length; i++) {
			var index = aVisibles[i].thumbIndex;
			var thumb = aVisibles[i].thumb;
			thumb.stopMotion();
			registerTween( "thumbMove" + index, new Tween( thumb, "_x", Strong.easeOut, thumb._x, nX, 0.5, true) );
			nX += thumb["mcMini"]._width + 5; // TOFIX= negrada total
		}
		mcPrev._visible = true;
		nPos --;
	}
	private function prevPosition():Void {
		var thumb = aVisibles[ aVisibles.length - 1 ].thumb;
		thumb.stopMotion();
		registerTween( "thumbMove" + aVisibles[ aVisibles.length - 1 ].thumbIndex, new Tween( thumb, "_x", Strong.easeOut, thumb._x, mcMask._x + mcMask._width + thumb._width, 0.5, true));
		aVisibles.pop();
		var prevThumb:Number = aVisibles[ 0 ].thumbIndex - 1;
		if ( aThumbs[prevThumb] ) aVisibles.unshift( { thumbIndex: prevThumb, thumb: aThumbs[ prevThumb ] } );
		else mcPrev._visible = false;
		var nX:Number = 0;
		for (var i:Number = 0; i < aVisibles.length; i++) {
			var index = aVisibles[i].thumbIndex;
			var thumb = aVisibles[i].thumb;
			thumb.stopMotion();
			registerTween( "thumbMove" + index, new Tween( thumb, "_x", Strong.easeOut, thumb._x, nX, 0.5, true));
			nX += thumb["mcMini"]._width + 5; // TOFIX= negrada total
		}
		nPos ++;
		checkScroll();
		if (nPos == 0) {
			mcPrev._visible = false;
		}
	}
	
	private function tweenFinished(tweenKey, tween) {
		MiradorPhotoGalleryThumb(tween.obj).initThumb();
		checkScroll();
	}
	
	
}